package com.ywgt.utils;

import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 输出JSON数据类
 */
public abstract class JSONAuthentication {
    /**
     * 输出Json信息
     * @param response
     * @param request
     * @param data
     * @throws IOException
     * @throws ServletException
     */
    public void WriteJSON(HttpServletResponse response, HttpServletRequest request,Object data)throws
            IOException, ServletException{
        response.setContentType("appliction/json;charset=UTF-8");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Method", "POST,GET");
        PrintWriter writer = response.getWriter();
        writer.println(new ObjectMapper().writeValueAsString(data));
        writer.flush();
        writer.close();
    }
}
